" Plugin
set nocompatible
filetype off
" vim-plug
call plug#begin('~/.vim/plugged')
Plug 'bling/vim-airline'
Plug 'w0rp/ale'
Plug 'scrooloose/nerdcommenter'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'majutsushi/tagbar'
Plug 'posva/vim-vue'
"Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'leafgarland/typescript-vim'
Plug 'Chiel92/vim-autoformat'
call plug#end()
"vim plug
set hidden
set cmdheight=2
set updatetime=300
set shortmess+=c
set signcolumn=yes
set nobackup
set noswapfile
syntax on
filetype plugin indent on
filetype plugin on
filetype indent on
syntax enable
syntax on
set number
set ruler
set tabstop=4
set softtabstop=4
set shiftwidth=4
set report=0
set expandtab
set autoindent
set hlsearch
set smarttab
set encoding=utf-8
"set cc=80,120
set scrolloff=5
let g:go_fmt_fail_silently = 1
let g:rainbow_active = 1
let g:ale_sign_column_always = 1
let g:ale_sign_error = '>>'
let g:ale_sign_warning = '--'
let g:ale_python_pylint_options = '--rcfile=/var/zhangyue/pylint.rcfile --disable=RP0401,RP0001,RP0002,RP0003,RP0101,RP0101,RP0701'
let g:ale_linters = {
\ 'javascript': ['eslint'],
\ 'typescript': ['eslint'],
\}
let g:tagbar_type_go = {
	\ 'ctagstype' : 'go',
	\ 'kinds'     : [
		\ 'p:package',
		\ 'i:imports:1',
		\ 'c:constants',
		\ 'v:variables',
		\ 't:types',
		\ 'n:interfaces',
		\ 'w:fields',
		\ 'e:embedded',
		\ 'm:methods',
		\ 'r:constructor',
		\ 'f:functions'
	\ ],
	\ 'sro' : '.',
	\ 'kind2scope' : {
		\ 't' : 'ctype',
		\ 'n' : 'ntype'
	\ },
	\ 'scope2kind' : {
		\ 'ctype' : 't',
		\ 'ntype' : 'n'
	\ },
	\ 'ctagsbin'  : 'gotags',
	\ 'ctagsargs' : '-sort -silent'
\ }
autocmd FileType javascript,vue setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
au FileType vue,javascript,toml,yml,typescript set tabstop=2
au FileType vue,javascript,toml,yml,typescript set softtabstop=2
au FileType vue,javascript,toml,yml,typescript set shiftwidth=2
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
au BufWritePost *.go silent! call RunGoFormat()
"autocmd FileType python setlocal omnifunc=jedi#completions
"color peachpuff_me
color molokai
set t_Co=256
set foldmethod=indent
set foldlevel=1
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"

" Remap keys for gotos
nmap <silent> gd <Plug>(coc-definition)
"nmap <silent> gy <Plug>(coc-type-definition)
"nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
nmap <silent> gs :call CocAction('jumpDefinition', 'vsplit')<CR>
nnoremap <silent> K :call <SID>show_documentation()<CR>
nnoremap <C-p> :FZF<cr>
nnoremap <F6> :TagbarToggle<cr>
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-H> <C-W><C-H>
nnoremap <C-L> <C-W><C-L>
nnoremap <space> za
map <silent><C-C> <Plug>NERDCommenterToggle
map <F5> :call CompileRun()<CR>
map <F4> :NERDTreeToggle<CR>
map <F3> :Autoformat<CR>
map <F2> :GitBlame<CR>
command Init call SetTitle()
command IniT call SetTestTitle()
command IniM call SetDBModelTitle()
command GitBlame call GitBlameCurrent()

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

function! s:show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  else
    call CocAction('doHover')
  endif
endfunction

func! CompileRun()
    exec "w"
    if &filetype == 'python'
        exec "!time python3 %"
    elseif &filetype == 'c' || &filetype == 'cpp'
        exec "!make"
        exec "!time ./%<"
    elseif &filetype == 'go'
        exec "!go run %"
    endif
endfunc

func! GitBlameCurrent()
    exec "!git blame %"
endfunc

func! RunGoFormat()
  exec "!go fmt % >/dev/null 2>&1"
  exec "e"
endfunc
